package com.apollographql.apollo.internal.interceptor;

import com.apollographql.apollo.api.FileUpload;
import com.apollographql.apollo.api.Input;
import com.apollographql.apollo.api.InputType;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseField;
import com.apollographql.apollo.api.ScalarTypeAdapters;
import com.apollographql.apollo.api.cache.http.HttpCachePolicy;
import com.apollographql.apollo.api.internal.ApolloLogger;
import com.apollographql.apollo.api.internal.InputFieldMarshaller;
import com.apollographql.apollo.api.internal.Optional;
import com.apollographql.apollo.api.internal.Utils;
import com.apollographql.apollo.api.internal.json.InputFieldJsonWriter;
import com.apollographql.apollo.api.internal.json.JsonWriter;
import com.apollographql.apollo.api.internal.network.ContentType;
import com.apollographql.apollo.cache.ApolloCacheHeaders;
import com.apollographql.apollo.cache.CacheHeaders;
import com.apollographql.apollo.exception.ApolloNetworkException;
import com.apollographql.apollo.interceptor.ApolloInterceptor;
import com.apollographql.apollo.interceptor.ApolloInterceptorChain;
import com.apollographql.apollo.request.RequestHeaders;
import com.apollographql.apollo.subscription.ApolloOperationMessageSerializer;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;
import com.kt.Metadata;
import com.kt.collections.CollectionsKt;
import com.kt.jvm.internal.DefaultConstructorMarker;
import com.kt.jvm.internal.Intrinsics;
import com.kt.text.StringsKt;
import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.ByteString;

@Metadata(bv = {1, 0, 3}, d1 = {"\u0000~\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\f\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\u0018\u0000 @2\u00020\u0001:\u0002@AB7\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\b\u0010\u0006\u001a\u0004\u0018\u00010\u0007\u0012\u0006\u0010\b\u001a\u00020\t\u0012\u0006\u0010\n\u001a\u00020\u000b\u0012\u0006\u0010\f\u001a\u00020\r¢\u0006\u0002\u0010\u000eJ2\u0010'\u001a\u00020(2\u0006\u0010)\u001a\u00020*2\u0012\u0010+\u001a\u000e\u0012\u0002\b\u0003\u0012\u0002\b\u0003\u0012\u0002\b\u00030,2\u0006\u0010-\u001a\u00020.2\u0006\u0010/\u001a\u000200J\b\u00101\u001a\u00020(H\u0016J\u0016\u00102\u001a\u00020(2\u0006\u00103\u001a\u0002042\u0006\u00105\u001a\u000206J:\u00107\u001a\u00020\u001b2\u0012\u0010+\u001a\u000e\u0012\u0002\b\u0003\u0012\u0002\b\u0003\u0012\u0002\b\u00030,2\u0006\u0010-\u001a\u00020.2\u0006\u0010/\u001a\u0002002\u0006\u00108\u001a\u00020\t2\u0006\u00109\u001a\u00020\tJ:\u0010:\u001a\u00020\u001b2\u0012\u0010+\u001a\u000e\u0012\u0002\b\u0003\u0012\u0002\b\u0003\u0012\u0002\b\u00030,2\u0006\u0010-\u001a\u00020.2\u0006\u0010/\u001a\u0002002\u0006\u00108\u001a\u00020\t2\u0006\u00109\u001a\u00020\tJ(\u0010;\u001a\u00020(2\u0006\u00103\u001a\u0002042\u0006\u0010<\u001a\u00020=2\u0006\u0010>\u001a\u00020?2\u0006\u00105\u001a\u000206H\u0016R\u0017\u0010\u0006\u001a\b\u0012\u0004\u0012\u00020\u00070\u000f¢\u0006\b\n\u0000\u001a\u0004\b\u0010\u0010\u0011R\u001a\u0010\u0012\u001a\u00020\tX\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0013\u0010\u0014\"\u0004\b\u0015\u0010\u0016R\u0011\u0010\u0004\u001a\u00020\u0005¢\u0006\b\n\u0000\u001a\u0004\b\u0017\u0010\u0018R\"\u0010\u0019\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\u001b0\u001aX\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u001c\u0010\u001d\"\u0004\b\u001e\u0010\u001fR\u0011\u0010\f\u001a\u00020\r¢\u0006\b\n\u0000\u001a\u0004\b \u0010!R\u0011\u0010\b\u001a\u00020\t¢\u0006\b\n\u0000\u001a\u0004\b\"\u0010\u0014R\u0011\u0010\n\u001a\u00020\u000b¢\u0006\b\n\u0000\u001a\u0004\b#\u0010$R\u0011\u0010\u0002\u001a\u00020\u0003¢\u0006\b\n\u0000\u001a\u0004\b%\u0010&¨\u0006B"}, d2 = {"Lcom/apollographql/apollo/internal/interceptor/ApolloServerInterceptor;", "Lcom/apollographql/apollo/interceptor/ApolloInterceptor;", "serverUrl", "Lokhttp3/HttpUrl;", "httpCallFactory", "Lokhttp3/Call$Factory;", "cachePolicy", "Lcom/apollographql/apollo/api/cache/http/HttpCachePolicy$Policy;", "prefetch", "", "scalarTypeAdapters", "Lcom/apollographql/apollo/api/ScalarTypeAdapters;", "logger", "Lcom/apollographql/apollo/api/internal/ApolloLogger;", "(Lokhttp3/HttpUrl;Lokhttp3/Call$Factory;Lcom/apollographql/apollo/api/cache/http/HttpCachePolicy$Policy;ZLcom/apollographql/apollo/api/ScalarTypeAdapters;Lcom/apollographql/apollo/api/internal/ApolloLogger;)V", "Lcom/apollographql/apollo/api/internal/Optional;", "getCachePolicy", "()Lcom/apollographql/apollo/api/internal/Optional;", "disposed", "getDisposed", "()Z", "setDisposed", "(Z)V", "getHttpCallFactory", "()Lokhttp3/Call$Factory;", "httpCallRef", "Ljava/util/concurrent/atomic/AtomicReference;", "Lokhttp3/Call;", "getHttpCallRef", "()Ljava/util/concurrent/atomic/AtomicReference;", "setHttpCallRef", "(Ljava/util/concurrent/atomic/AtomicReference;)V", "getLogger", "()Lcom/apollographql/apollo/api/internal/ApolloLogger;", "getPrefetch", "getScalarTypeAdapters", "()Lcom/apollographql/apollo/api/ScalarTypeAdapters;", "getServerUrl", "()Lokhttp3/HttpUrl;", "decorateRequest", "", "requestBuilder", "Lokhttp3/Request$Builder;", "operation", "Lcom/apollographql/apollo/api/Operation;", "cacheHeaders", "Lcom/apollographql/apollo/cache/CacheHeaders;", "requestHeaders", "Lcom/apollographql/apollo/request/RequestHeaders;", "dispose", "executeHttpCall", "request", "Lcom/apollographql/apollo/interceptor/ApolloInterceptor$InterceptorRequest;", "callBack", "Lcom/apollographql/apollo/interceptor/ApolloInterceptor$CallBack;", "httpGetCall", "writeQueryDocument", "autoPersistQueries", "httpPostCall", "interceptAsync", "chain", "Lcom/apollographql/apollo/interceptor/ApolloInterceptorChain;", "dispatcher", "Ljava/util/concurrent/Executor;", "Companion", "FileUploadMeta", "apollo-runtime"}, k = 1, mv = {1, 4, 0})
/* compiled from: ApolloServerInterceptor.kt */
public final class ApolloServerInterceptor implements ApolloInterceptor {
    public static final String ACCEPT_TYPE = "application/json";
    public static final String CONTENT_TYPE = "application/json";
    public static final Companion Companion = new Companion(null);
    public static final String HEADER_ACCEPT_TYPE = "Accept";
    public static final String HEADER_APOLLO_OPERATION_ID = "X-APOLLO-OPERATION-ID";
    public static final String HEADER_APOLLO_OPERATION_NAME = "X-APOLLO-OPERATION-NAME";
    public static final String HEADER_CONTENT_TYPE = "Content-Type";
    private static final MediaType MEDIA_TYPE = MediaType.parse(ContentType.APPLICATION_JSON_UTF8);
    private final Optional<HttpCachePolicy.Policy> cachePolicy;
    private volatile boolean disposed;
    private final Call.Factory httpCallFactory;
    private AtomicReference<Call> httpCallRef = new AtomicReference<>();
    private final ApolloLogger logger;
    private final boolean prefetch;
    private final ScalarTypeAdapters scalarTypeAdapters;
    private final HttpUrl serverUrl;

    public ApolloServerInterceptor(HttpUrl serverUrl2, Call.Factory httpCallFactory2, HttpCachePolicy.Policy cachePolicy2, boolean prefetch2, ScalarTypeAdapters scalarTypeAdapters2, ApolloLogger logger2) {
        Intrinsics.checkParameterIsNotNull(serverUrl2, "serverUrl");
        Intrinsics.checkParameterIsNotNull(httpCallFactory2, "httpCallFactory");
        Intrinsics.checkParameterIsNotNull(scalarTypeAdapters2, "scalarTypeAdapters");
        Intrinsics.checkParameterIsNotNull(logger2, "logger");
        this.serverUrl = (HttpUrl) Utils.checkNotNull(serverUrl2, "serverUrl == null");
        this.httpCallFactory = (Call.Factory) Utils.checkNotNull(httpCallFactory2, "httpCallFactory == null");
        Optional<HttpCachePolicy.Policy> fromNullable = Optional.fromNullable(cachePolicy2);
        Intrinsics.checkExpressionValueIsNotNull(fromNullable, "Optional.fromNullable(cachePolicy)");
        this.cachePolicy = fromNullable;
        this.prefetch = prefetch2;
        this.scalarTypeAdapters = (ScalarTypeAdapters) Utils.checkNotNull(scalarTypeAdapters2, "scalarTypeAdapters == null");
        this.logger = (ApolloLogger) Utils.checkNotNull(logger2, "logger == null");
    }

    public final HttpUrl getServerUrl() {
        return this.serverUrl;
    }

    public final Call.Factory getHttpCallFactory() {
        return this.httpCallFactory;
    }

    public final Optional<HttpCachePolicy.Policy> getCachePolicy() {
        return this.cachePolicy;
    }

    public final boolean getPrefetch() {
        return this.prefetch;
    }

    public final ApolloLogger getLogger() {
        return this.logger;
    }

    public final ScalarTypeAdapters getScalarTypeAdapters() {
        return this.scalarTypeAdapters;
    }

    public final AtomicReference<Call> getHttpCallRef() {
        return this.httpCallRef;
    }

    public final void setHttpCallRef(AtomicReference<Call> atomicReference) {
        Intrinsics.checkParameterIsNotNull(atomicReference, "<set-?>");
        this.httpCallRef = atomicReference;
    }

    public final boolean getDisposed() {
        return this.disposed;
    }

    public final void setDisposed(boolean z) {
        this.disposed = z;
    }

    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
    public void interceptAsync(InterceptorRequest request, ApolloInterceptorChain chain, Executor dispatcher, CallBack callBack) {
        Intrinsics.checkParameterIsNotNull(request, "request");
        Intrinsics.checkParameterIsNotNull(chain, "chain");
        Intrinsics.checkParameterIsNotNull(dispatcher, "dispatcher");
        Intrinsics.checkParameterIsNotNull(callBack, "callBack");
        dispatcher.execute(new ApolloServerInterceptor$interceptAsync$1(this, request, callBack));
    }

    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
    public void dispose() {
        this.disposed = true;
        Call httpCall = this.httpCallRef.getAndSet(null);
        if (httpCall != null) {
            httpCall.cancel();
        }
    }

    public final void executeHttpCall(InterceptorRequest request, CallBack callBack) {
        Call httpCall;
        Intrinsics.checkParameterIsNotNull(request, "request");
        Intrinsics.checkParameterIsNotNull(callBack, "callBack");
        if (!this.disposed) {
            callBack.onFetch(FetchSourceType.NETWORK);
            try {
                if (!request.useHttpGetMethodForQueries || !(request.operation instanceof Query)) {
                    Operation<?, ?, ?> operation = request.operation;
                    Intrinsics.checkExpressionValueIsNotNull(operation, "request.operation");
                    CacheHeaders cacheHeaders = request.cacheHeaders;
                    Intrinsics.checkExpressionValueIsNotNull(cacheHeaders, "request.cacheHeaders");
                    RequestHeaders requestHeaders = request.requestHeaders;
                    Intrinsics.checkExpressionValueIsNotNull(requestHeaders, "request.requestHeaders");
                    httpCall = httpPostCall(operation, cacheHeaders, requestHeaders, request.sendQueryDocument, request.autoPersistQueries);
                } else {
                    Operation<?, ?, ?> operation2 = request.operation;
                    CacheHeaders cacheHeaders2 = request.cacheHeaders;
                    Intrinsics.checkExpressionValueIsNotNull(cacheHeaders2, "request.cacheHeaders");
                    RequestHeaders requestHeaders2 = request.requestHeaders;
                    Intrinsics.checkExpressionValueIsNotNull(requestHeaders2, "request.requestHeaders");
                    httpCall = httpGetCall(operation2, cacheHeaders2, requestHeaders2, request.sendQueryDocument, request.autoPersistQueries);
                }
                Call previousCall = this.httpCallRef.getAndSet(httpCall);
                if (previousCall != null) {
                    previousCall.cancel();
                }
                if (httpCall.isCanceled() || this.disposed) {
                    this.httpCallRef.compareAndSet(httpCall, null);
                } else {
                    httpCall.enqueue(new ApolloServerInterceptor$executeHttpCall$1(this, httpCall, request, callBack));
                }
            } catch (IOException e) {
                this.logger.e(e, "Failed to prepare http call for operation %s", request.operation.name().name());
                callBack.onFailure(new ApolloNetworkException("Failed to prepare http call", e));
            }
        }
    }

    public final Call httpGetCall(Operation<?, ?, ?> operation, CacheHeaders cacheHeaders, RequestHeaders requestHeaders, boolean writeQueryDocument, boolean autoPersistQueries) throws IOException {
        Intrinsics.checkParameterIsNotNull(operation, "operation");
        Intrinsics.checkParameterIsNotNull(cacheHeaders, "cacheHeaders");
        Intrinsics.checkParameterIsNotNull(requestHeaders, "requestHeaders");
        Request.Builder requestBuilder = new Request.Builder().url(Companion.httpGetUrl(this.serverUrl, operation, this.scalarTypeAdapters, writeQueryDocument, autoPersistQueries)).get();
        Intrinsics.checkExpressionValueIsNotNull(requestBuilder, "requestBuilder");
        decorateRequest(requestBuilder, operation, cacheHeaders, requestHeaders);
        Call newCall = this.httpCallFactory.newCall(requestBuilder.build());
        Intrinsics.checkExpressionValueIsNotNull(newCall, "httpCallFactory.newCall(requestBuilder.build())");
        return newCall;
    }

    public final Call httpPostCall(Operation<?, ?, ?> operation, CacheHeaders cacheHeaders, RequestHeaders requestHeaders, boolean writeQueryDocument, boolean autoPersistQueries) throws IOException {
        Intrinsics.checkParameterIsNotNull(operation, "operation");
        Intrinsics.checkParameterIsNotNull(cacheHeaders, "cacheHeaders");
        Intrinsics.checkParameterIsNotNull(requestHeaders, "requestHeaders");
        MediaType mediaType = MEDIA_TYPE;
        Companion companion = Companion;
        Request.Builder requestBuilder = new Request.Builder().url(this.serverUrl).header(HEADER_CONTENT_TYPE, "application/json").post(companion.transformToMultiPartIfUploadExists(RequestBody.create(mediaType, companion.httpPostRequestBody(operation, this.scalarTypeAdapters, writeQueryDocument, autoPersistQueries)), operation));
        Intrinsics.checkExpressionValueIsNotNull(requestBuilder, "requestBuilder");
        decorateRequest(requestBuilder, operation, cacheHeaders, requestHeaders);
        Call newCall = this.httpCallFactory.newCall(requestBuilder.build());
        Intrinsics.checkExpressionValueIsNotNull(newCall, "httpCallFactory.newCall(requestBuilder.build())");
        return newCall;
    }

    public final void decorateRequest(Request.Builder requestBuilder, Operation<?, ?, ?> operation, CacheHeaders cacheHeaders, RequestHeaders requestHeaders) throws IOException {
        Intrinsics.checkParameterIsNotNull(requestBuilder, "requestBuilder");
        Intrinsics.checkParameterIsNotNull(operation, "operation");
        Intrinsics.checkParameterIsNotNull(cacheHeaders, "cacheHeaders");
        Intrinsics.checkParameterIsNotNull(requestHeaders, "requestHeaders");
        requestBuilder.header(HEADER_ACCEPT_TYPE, "application/json").header(HEADER_APOLLO_OPERATION_ID, operation.operationId()).header(HEADER_APOLLO_OPERATION_NAME, operation.name().name()).tag(operation.operationId());
        for (String header : requestHeaders.headers()) {
            requestBuilder.header(header, requestHeaders.headerValue(header));
        }
        if (this.cachePolicy.isPresent()) {
            HttpCachePolicy.Policy cachePolicy2 = this.cachePolicy.get();
            requestBuilder.header("X-APOLLO-CACHE-KEY", Companion.cacheKey(operation, this.scalarTypeAdapters)).header("X-APOLLO-CACHE-FETCH-STRATEGY", cachePolicy2.fetchStrategy.name()).header("X-APOLLO-EXPIRE-TIMEOUT", String.valueOf(cachePolicy2.expireTimeoutMs())).header("X-APOLLO-EXPIRE-AFTER-READ", Boolean.toString(cachePolicy2.expireAfterRead)).header("X-APOLLO-PREFETCH", Boolean.toString(this.prefetch)).header("X-APOLLO-CACHE-DO-NOT-STORE", Boolean.toString(StringsKt.equals("true", cacheHeaders.headerValue(ApolloCacheHeaders.DO_NOT_STORE), true)));
        }
    }

    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u001a\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0007\u0018\u00002\u00020\u0001B\u001f\b\u0000\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0003\u0012\u0006\u0010\u0005\u001a\u00020\u0006¢\u0006\u0002\u0010\u0007R\u0011\u0010\u0005\u001a\u00020\u0006¢\u0006\b\n\u0000\u001a\u0004\b\b\u0010\tR\u0011\u0010\u0002\u001a\u00020\u0003¢\u0006\b\n\u0000\u001a\u0004\b\n\u0010\u000bR\u0011\u0010\u0004\u001a\u00020\u0003¢\u0006\b\n\u0000\u001a\u0004\b\f\u0010\u000b¨\u0006\r"}, d2 = {"Lcom/apollographql/apollo/internal/interceptor/ApolloServerInterceptor$FileUploadMeta;", "", "key", "", "mimetype", "fileUpload", "Lcom/apollographql/apollo/api/FileUpload;", "(Ljava/lang/String;Ljava/lang/String;Lcom/apollographql/apollo/api/FileUpload;)V", "getFileUpload", "()Lcom/apollographql/apollo/api/FileUpload;", "getKey", "()Ljava/lang/String;", "getMimetype", "apollo-runtime"}, k = 1, mv = {1, 4, 0})
    /* compiled from: ApolloServerInterceptor.kt */
    public static final class FileUploadMeta {
        private final FileUpload fileUpload;
        private final String key;
        private final String mimetype;

        public FileUploadMeta(String key2, String mimetype2, FileUpload fileUpload2) {
            Intrinsics.checkParameterIsNotNull(key2, "key");
            Intrinsics.checkParameterIsNotNull(mimetype2, "mimetype");
            Intrinsics.checkParameterIsNotNull(fileUpload2, "fileUpload");
            this.key = key2;
            this.mimetype = mimetype2;
            this.fileUpload = fileUpload2;
        }

        public final FileUpload getFileUpload() {
            return this.fileUpload;
        }

        public final String getKey() {
            return this.key;
        }

        public final String getMimetype() {
            return this.mimetype;
        }
    }

    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000b\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0007\b\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\"\u0010\u000e\u001a\u00020\u000f2\u0006\u0010\u0010\u001a\u00020\u00112\u0012\u0010\u0012\u001a\u000e\u0012\u0002\b\u0003\u0012\u0002\b\u0003\u0012\u0002\b\u00030\u0013J,\u0010\u0014\u001a\u00020\u000f2\u0006\u0010\u0010\u001a\u00020\u00112\u0012\u0010\u0012\u001a\u000e\u0012\u0002\b\u0003\u0012\u0002\b\u0003\u0012\u0002\b\u00030\u00132\b\u0010\u0015\u001a\u0004\u0018\u00010\u0016J$\u0010\u0017\u001a\u00020\u00042\u0012\u0010\u0012\u001a\u000e\u0012\u0002\b\u0003\u0012\u0002\b\u0003\u0012\u0002\b\u00030\u00132\b\u0010\u0015\u001a\u0004\u0018\u00010\u0016J<\u0010\u0018\u001a\u00020\u00192\u0006\u0010\u001a\u001a\u00020\u00192\u0012\u0010\u0012\u001a\u000e\u0012\u0002\b\u0003\u0012\u0002\b\u0003\u0012\u0002\b\u00030\u00132\b\u0010\u0015\u001a\u0004\u0018\u00010\u00162\u0006\u0010\u001b\u001a\u00020\u001c2\u0006\u0010\u001d\u001a\u00020\u001cJ\u001e\u0010\u001e\u001a\u00020\u001f2\b\u0010 \u001a\u0004\u0018\u00010\u001f2\f\u0010!\u001a\b\u0012\u0004\u0012\u00020#0\"J4\u0010$\u001a\u00020%2\u0012\u0010\u0012\u001a\u000e\u0012\u0002\b\u0003\u0012\u0002\b\u0003\u0012\u0002\b\u00030\u00132\b\u0010\u0015\u001a\u0004\u0018\u00010\u00162\u0006\u0010\u001b\u001a\u00020\u001c2\u0006\u0010\u001d\u001a\u00020\u001cJ(\u0010&\u001a\u00020\u000f2\b\u0010'\u001a\u0004\u0018\u00010\u00012\u0006\u0010(\u001a\u00020\u00042\f\u0010)\u001a\b\u0012\u0004\u0012\u00020#0\"H\u0002J&\u0010*\u001a\u0004\u0018\u00010\u001f2\b\u0010+\u001a\u0004\u0018\u00010\u001f2\u0012\u0010\u0012\u001a\u000e\u0012\u0002\b\u0003\u0012\u0002\b\u0003\u0012\u0002\b\u00030\u0013R\u000e\u0010\u0003\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\u0005\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\u0007\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u0013\u0010\n\u001a\u0004\u0018\u00010\u000b¢\u0006\b\n\u0000\u001a\u0004\b\f\u0010\r¨\u0006,"}, d2 = {"Lcom/apollographql/apollo/internal/interceptor/ApolloServerInterceptor$Companion;", "", "()V", "ACCEPT_TYPE", "", "CONTENT_TYPE", "HEADER_ACCEPT_TYPE", "HEADER_APOLLO_OPERATION_ID", "HEADER_APOLLO_OPERATION_NAME", "HEADER_CONTENT_TYPE", "MEDIA_TYPE", "Lokhttp3/MediaType;", "getMEDIA_TYPE", "()Lokhttp3/MediaType;", "addExtensionsUrlQueryParameter", "", "urlBuilder", "Lokhttp3/HttpUrl$Builder;", "operation", "Lcom/apollographql/apollo/api/Operation;", "addVariablesUrlQueryParameter", "scalarTypeAdapters", "Lcom/apollographql/apollo/api/ScalarTypeAdapters;", "cacheKey", "httpGetUrl", "Lokhttp3/HttpUrl;", "serverUrl", "writeQueryDocument", "", "autoPersistQueries", "httpMultipartRequestBody", "Lokhttp3/RequestBody;", "operations", "fileUploadMetaList", "Ljava/util/ArrayList;", "Lcom/apollographql/apollo/internal/interceptor/ApolloServerInterceptor$FileUploadMeta;", "httpPostRequestBody", "Lokio/ByteString;", "recursiveGetUploadData", "value", ResponseField.VARIABLE_NAME_KEY, "allUploads", "transformToMultiPartIfUploadExists", "originalBody", "apollo-runtime"}, k = 1, mv = {1, 4, 0})
    /* compiled from: ApolloServerInterceptor.kt */
    public static final class Companion {
        private Companion() {
        }

        public /* synthetic */ Companion(DefaultConstructorMarker $constructor_marker) {
            this();
        }

        public final MediaType getMEDIA_TYPE() {
            return ApolloServerInterceptor.MEDIA_TYPE;
        }

        public final String cacheKey(Operation<?, ?, ?> operation, ScalarTypeAdapters scalarTypeAdapters) throws IOException {
            Intrinsics.checkParameterIsNotNull(operation, "operation");
            return httpPostRequestBody(operation, scalarTypeAdapters, true, true).md5().hex();
        }

        public final ByteString httpPostRequestBody(Operation<?, ?, ?> operation, ScalarTypeAdapters scalarTypeAdapters, boolean writeQueryDocument, boolean autoPersistQueries) throws IOException {
            Intrinsics.checkParameterIsNotNull(operation, "operation");
            if (scalarTypeAdapters == null) {
                Intrinsics.throwNpe();
            }
            return operation.composeRequestBody(autoPersistQueries, writeQueryDocument, scalarTypeAdapters);
        }

        public final HttpUrl httpGetUrl(HttpUrl serverUrl, Operation<?, ?, ?> operation, ScalarTypeAdapters scalarTypeAdapters, boolean writeQueryDocument, boolean autoPersistQueries) throws IOException {
            Intrinsics.checkParameterIsNotNull(serverUrl, "serverUrl");
            Intrinsics.checkParameterIsNotNull(operation, "operation");
            HttpUrl.Builder urlBuilder = serverUrl.newBuilder();
            if (!autoPersistQueries || writeQueryDocument) {
                urlBuilder.addQueryParameter(ApolloOperationMessageSerializer.JSON_KEY_QUERY, operation.queryDocument());
            }
            if (operation.variables() != Operation.EMPTY_VARIABLES) {
                Intrinsics.checkExpressionValueIsNotNull(urlBuilder, "urlBuilder");
                addVariablesUrlQueryParameter(urlBuilder, operation, scalarTypeAdapters);
            }
            urlBuilder.addQueryParameter(ApolloOperationMessageSerializer.JSON_KEY_OPERATION_NAME, operation.name().name());
            if (autoPersistQueries) {
                Intrinsics.checkExpressionValueIsNotNull(urlBuilder, "urlBuilder");
                addExtensionsUrlQueryParameter(urlBuilder, operation);
            }
            HttpUrl build = urlBuilder.build();
            Intrinsics.checkExpressionValueIsNotNull(build, "urlBuilder.build()");
            return build;
        }

        public final void addVariablesUrlQueryParameter(HttpUrl.Builder urlBuilder, Operation<?, ?, ?> operation, ScalarTypeAdapters scalarTypeAdapters) throws IOException {
            Intrinsics.checkParameterIsNotNull(urlBuilder, "urlBuilder");
            Intrinsics.checkParameterIsNotNull(operation, "operation");
            Buffer buffer = new Buffer();
            JsonWriter jsonWriter = JsonWriter.Companion.of(buffer);
            jsonWriter.setSerializeNulls(true);
            jsonWriter.beginObject();
            InputFieldMarshaller marshaller = operation.variables().marshaller();
            if (scalarTypeAdapters == null) {
                Intrinsics.throwNpe();
            }
            marshaller.marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters));
            jsonWriter.endObject();
            jsonWriter.close();
            urlBuilder.addQueryParameter(ApolloOperationMessageSerializer.JSON_KEY_VARIABLES, buffer.readUtf8());
        }

        public final void addExtensionsUrlQueryParameter(HttpUrl.Builder urlBuilder, Operation<?, ?, ?> operation) throws IOException {
            Intrinsics.checkParameterIsNotNull(urlBuilder, "urlBuilder");
            Intrinsics.checkParameterIsNotNull(operation, "operation");
            Buffer buffer = new Buffer();
            JsonWriter jsonWriter = JsonWriter.Companion.of(buffer);
            jsonWriter.setSerializeNulls(true);
            jsonWriter.beginObject();
            jsonWriter.name(ApolloOperationMessageSerializer.JSON_KEY_EXTENSIONS_PERSISTED_QUERY).beginObject().name(ApolloOperationMessageSerializer.JSON_KEY_EXTENSIONS_PERSISTED_QUERY_VERSION).value(1L).name(ApolloOperationMessageSerializer.JSON_KEY_EXTENSIONS_PERSISTED_QUERY_HASH).value(operation.operationId()).endObject();
            jsonWriter.endObject();
            jsonWriter.close();
            urlBuilder.addQueryParameter(ApolloOperationMessageSerializer.JSON_KEY_EXTENSIONS, buffer.readUtf8());
        }

        /* JADX INFO: Multiple debug info for r3v1 'index$iv'  int: [D('index' int), D('index$iv' int)] */
        private final void recursiveGetUploadData(Object value, String variableName, ArrayList<FileUploadMeta> arrayList) {
            int i = 0;
            if (value instanceof InputType) {
                try {
                    Field[] fields = ((InputType) value).getClass().getDeclaredFields();
                    int length = fields.length;
                    while (i < length) {
                        Field field = fields[i];
                        Intrinsics.checkExpressionValueIsNotNull(field, "field");
                        field.setAccessible(true);
                        recursiveGetUploadData(field.get(value), variableName + '.' + field.getName(), arrayList);
                        i++;
                    }
                } catch (IllegalAccessException e) {
                }
            } else if (value instanceof Input) {
                recursiveGetUploadData(((Input) value).value, variableName, arrayList);
            } else if (value instanceof FileUpload) {
                arrayList.add(new FileUploadMeta(variableName, ((FileUpload) value).getMimetype(), (FileUpload) value));
            } else if (value instanceof Object[]) {
                int varFileIndex = 0;
                Object[] $this$filterIsInstance$iv = (Object[]) value;
                Collection destination$iv$iv = new ArrayList();
                int length2 = $this$filterIsInstance$iv.length;
                while (i < length2) {
                    Object element$iv$iv = $this$filterIsInstance$iv[i];
                    if (element$iv$iv instanceof FileUpload) {
                        destination$iv$iv.add(element$iv$iv);
                    }
                    i++;
                }
                for (Object upload : (List) destination$iv$iv) {
                    FileUpload upload1= (FileUpload) upload;
                    String key = variableName + '.' + varFileIndex;
                    arrayList.add(new FileUploadMeta(key, upload1.getMimetype(), upload1));
                    System.out.println((Object) key);
                    varFileIndex++;
                }
            } else if (value instanceof Collection) {
                int index = 0;
                for (Object item$iv : (Iterable) value) {
                    int index$iv = index + 1;
                    if (index < 0) {
                        CollectionsKt.throwIndexOverflow();
                    }
                    ApolloServerInterceptor.Companion.recursiveGetUploadData(item$iv, variableName + '.' + index, arrayList);
                    index = index$iv;
                }
            }
        }

        public final RequestBody transformToMultiPartIfUploadExists(RequestBody originalBody, Operation<?, ?, ?> operation) throws IOException {
            Intrinsics.checkParameterIsNotNull(operation, "operation");
            ArrayList allUploads = new ArrayList();
            for (String variableName : operation.variables().valueMap().keySet()) {
                Object value = operation.variables().valueMap().get(variableName);
                recursiveGetUploadData(value, "variables." + variableName, allUploads);
            }
            if (allUploads.isEmpty()) {
                return originalBody;
            }
            return httpMultipartRequestBody(originalBody, allUploads);
        }

        /* JADX INFO: Multiple debug info for r5v1 'index$iv'  int: [D('i' int), D('index$iv' int)] */
        /* JADX INFO: Multiple debug info for r8v1 'index$iv'  int: [D('index$iv' int), D('i' int)] */
        public final RequestBody httpMultipartRequestBody(RequestBody operations, ArrayList<FileUploadMeta> $this$forEachIndexed$iv) throws IOException {
            Buffer buffer;
            Intrinsics.checkParameterIsNotNull($this$forEachIndexed$iv, "fileUploadMetaList");
            Buffer buffer2 = new Buffer();
            JsonWriter jsonWriter = JsonWriter.Companion.of(buffer2);
            jsonWriter.beginObject();
            int i = 0;
            for (FileUploadMeta item$iv : $this$forEachIndexed$iv) {
                int index$iv = i + 1;
                if (i < 0) {
                    CollectionsKt.throwIndexOverflow();
                }
                jsonWriter.name(String.valueOf(i)).beginArray();
                jsonWriter.value(item$iv.getKey());
                jsonWriter.endArray();
                i = index$iv;
            }
            jsonWriter.endObject();
            jsonWriter.close();
            MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("operations", null, operations).addFormDataPart("map", null, RequestBody.create(getMEDIA_TYPE(), buffer2.readByteString()));
            int index$iv2 = 0;
            for (FileUploadMeta item$iv2 : $this$forEachIndexed$iv) {
                int index$iv3 = index$iv2 + 1;
                if (index$iv2 < 0) {
                    CollectionsKt.throwIndexOverflow();
                }
                FileUploadMeta fileUploadMeta = item$iv2;
                String it = fileUploadMeta.getFileUpload().getFilePath();
                File file = it != null ? new File(it) : null;
                MediaType mimetype = MediaType.parse(fileUploadMeta.getFileUpload().getMimetype());
                if (file != null) {
                    buffer = buffer2;
                    multipartBodyBuilder.addFormDataPart(String.valueOf(index$iv2), file.getName(), RequestBody.create(mimetype, file));
                } else {
                    buffer = buffer2;
                    multipartBodyBuilder.addFormDataPart(String.valueOf(index$iv2), fileUploadMeta.getFileUpload().fileName(), new ApolloServerInterceptor$Companion$httpMultipartRequestBody$2$1(mimetype, fileUploadMeta));
                }
                index$iv2 = index$iv3;
                buffer2 = buffer;
            }
            MultipartBody build = multipartBodyBuilder.build();
            Intrinsics.checkExpressionValueIsNotNull(build, "multipartBodyBuilder.build()");
            return build;
        }
    }
}
